package com.openness.spider.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Rain
 * @version 2013-7-21 下午4:38:01
 */

public class TimeUtil {

	private static SimpleDateFormat SDF = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static String longToString(long time) {
		return SDF.format(new Date(time));
	}

	public static long stringToLong(String time) throws ParseException {
		return SDF.parse(time).getTime();
	}

	public static String[] getTimeRange(long time) {
		Calendar ctime = Calendar.getInstance();

		Calendar cbegin = Calendar.getInstance();

		Calendar cend = Calendar.getInstance();

		ctime.setTimeInMillis(time);

		cend.set(ctime.get(Calendar.YEAR), ctime.get(Calendar.MONTH),
				ctime.get(Calendar.DAY_OF_MONTH),
				ctime.get(Calendar.HOUR_OF_DAY), 0, 0);

		ctime.add(Calendar.HOUR, -1);

		cbegin.set(ctime.get(Calendar.YEAR), ctime.get(Calendar.MONTH),
				ctime.get(Calendar.DAY_OF_MONTH),
				ctime.get(Calendar.HOUR_OF_DAY), 0, 0);

		String[] timeRanges = new String[2];

		timeRanges[0] = String.valueOf(cbegin.getTime().getTime());
		timeRanges[1] = String.valueOf(cend.getTime().getTime());

		return timeRanges;
	}

	public static void main(String[] args) {
		long now = System.currentTimeMillis();

		String[] timeRange = TimeUtil.getTimeRange(now);

		System.out
				.println("begin: " + longToString(Long.valueOf(timeRange[0])));

		System.out.println("end: " + longToString(Long.valueOf(timeRange[1])));
	}

}
